<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class HomeModel extends Model
{
	// 获取首页导航
    static public function getNav(){
    	return DB::table('article_category')->select('id','type_name','type_url','type_nid')->where(['is_hidden'=>0,'pid'=>0])->orderBy('sort_order','asc')->get();
    }
    // 获取地区列表
    static public function getAreaList($map = array()){
    	return DB::table('area')->where($map)->orderBy('sort_order','asc')->get();
    }
    // 获取网站参数
    static public function getGlobal(){
    	return DB::table('global')->orderBy('sort_order','desc')->get();
    }
    // 获取网站参数的值
    static public function getGlobalVal(){
        $global = DB::table('global')->orderBy('sort_order','desc')->get();
        $res = array();
        // dd($global);exit;
        foreach($global as $v){
            $res[$v->code] = $v->text;
        }
        return $res;
    }
    // 获取分类
    static public function getProductCate(){
		return DB::table('product_category')->where(['pid'=>0])->orderBy('sort_order','desc')->get();
    }
    // 获取单行产品数据
    static public function getProduct($field=array(),$map=array()){
        return DB::table('product')->select($field)->where($map)->first();
    }
    // 获取产品数据列表
    static public function getProducts($field=array(),$map=array(),$count=12){
        if(!empty($field)){
            if(!empty($map)){
                return DB::table('product')->select($field)->where($map)->orderBy('sort_order','desc')->paginate($count);
            }else{
                return DB::table('product')->select($field)->orderBy('sort_order','desc')->paginate($count);
            }
        }else{
            if(!empty($map)){
                return DB::table('product')->where($map)->orderBy('sort_order','desc')->paginate($count);
            }else{
                return DB::table('product')->orderBy('sort_order','desc')->paginate($count);
            }
        }
        
    }
    // 获取单个产品图片列表
    static public function getImages($field=array(),$map=array()){
        if(!empty($field)){
            if(!empty($map)){
                return DB::table('product_img')->select($field)->where($map)->orderBy('id','asc')->get();
            }else{
                return DB::table('product_img')->select($field)->orderBy('id','asc')->get();
            }
        }else{
            if(!empty($map)){
                return DB::table('product_img')->where($map)->orderBy('id','asc')->get();
            }else{
                return DB::table('product_img')->orderBy('id','asc')->get();
            }
        }
        
    }
    // 获取单个产品图片列表
    static public function getImage($field=array(),$map=array()){
        if(!empty($field)){
            if(!empty($map)){
                return DB::table('product_img')->select($field)->where($map)->orderBy('id','asc')->first();
            }else{
                return DB::table('product_img')->select($field)->orderBy('id','asc')->first();
            }
        }else{
            if(!empty($map)){
                return DB::table('product_img')->where($map)->orderBy('id','asc')->first();
            }else{
                return DB::table('product_img')->orderBy('id','asc')->first();
            }
        }
        
    }
    // 注册一个用户
    static public function addUser($data){
        $newid = DB::table('user')->insertGetId($data);
        if($newid){
            DB::table('user_money')->insert(['uid'=>$newid]);
        }
        return $newid;
    }
    // 获取一个用户
    static public function getOneUser($map){
        return DB::table(DB::raw('zzb_user u'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('u.id'), '=', DB::raw('m.uid'))->where($map)->first();
    }



}
